/*
 *    该类继承自QThread, 但目的是同时提供阻塞的操作和基于独立线程的操作.
 *
 *    由于Thrift's Client的工作模型是几层抽象(TTransport, TProtocol )协同来完成工作的,
 * SO, qzm加一层封装, 开始命名为ThriftClient, 但由于和thrift generate生成的Thrift.h中的class ThriftClient命名冲突,
 * SO, 改名为ThriftClientWrap.
 *
 * */

#include "ThriftClientWrap.h"
#include "./qzmthrift/thrift_generate/Thrift.h"
#include <thrift/protocol/TBinaryProtocol.h>
#include <thrift/transport/TSocket.h>
#include <thrift/transport/TTransportUtils.h>
#include <QDebug>
#include <iostream>

using namespace ::apache::thrift;
using namespace ::apache::thrift::protocol;
using namespace ::apache::thrift::transport;
using namespace std;

ThriftClientWrap::ThriftClientWrap(QObject *parent) : QThread(parent)
{
#if 0
    m_isRun = true;
    this->start();
#endif
}

//V1.0.0
//The most common use of thrift is the blocking model.
int ThriftClientWrap::openRtBCST_Block()
{
    int ret=0;

    std::shared_ptr<TTransport> socket(new TSocket("localhost", 9090));
    std::shared_ptr<TTransport> transport(new TBufferedTransport(socket));
    std::shared_ptr<TProtocol>  protocol(new TBinaryProtocol(transport));
    ThriftClient client(protocol);

    try
    {
        transport->open();
        ret = client.openRtBCST();
        transport->close();
    } catch (TException& tx) {
        qDebug()<<"ERROR: " << tx.what() << endl;
        transport->close();//KEY,if no-existent,when Server quit,\
            and Server run again,Client call Server's Method would not sucess.
        ret = -1;
    }

    return ret;
}

//V1.0.0
//The most common use of thrift is the blocking model.
int ThriftClientWrap::closeRtBCST_Block()
{
     int ret=0;

    std::shared_ptr<TTransport> socket(new TSocket("localhost", 9090));
    std::shared_ptr<TTransport> transport(new TBufferedTransport(socket));
    std::shared_ptr<TProtocol>  protocol(new TBinaryProtocol(transport));
    ThriftClient client(protocol);

    try
    {
        transport->open();
        ret = client.closeRtBCST();
        transport->close();
    } catch (TException& tx) {
        qDebug()<<"ERROR: " << tx.what() << endl;
        transport->close();//KEY,if no-existent,when Server quit,\
            and Server run again,Client call Server's Method would not sucess.
        ret = -1;
    }

    return ret;
}

void ThriftClientWrap::disableRun()
{
    m_isRun = false;
}

void ThriftClientWrap::run()
{
    qDebug()<<"Enter ThriftWrap::run()";

    std::shared_ptr<TTransport> socket(new TSocket("localhost", 9090));
    std::shared_ptr<TTransport> transport(new TBufferedTransport(socket));
    std::shared_ptr<TProtocol>  protocol(new TBinaryProtocol(transport));
    ThriftClient client(protocol);

    qDebug()<<"\nCreate sockets,transports,protocols."<<endl;
    while(m_isRun)
    {
        try
        {
            transport->open();
            int ret_val1 = client.openRtBCST();
            qDebug()<<"ret_vall="<<ret_val1;
            sleep(1);
            int ret_val2 = client.closeRtBCST();
            qDebug()<<"ret_val2="<<ret_val2;
            sleep(1);
            transport->close();
        } catch (TException& tx) {
            qDebug()<<"ERROR: " << tx.what() << endl;
            transport->close();//KEY,if no-existent,when Server quit,\
            and Server run again,Client call Server's Method would not sucess.
        }
    }
}
